var express = require('express')
var router = express.Router()
const { getAll, getOne, exec } = require('../../db/index')

router.get('/type/:id', async (req, res) => {
  const { id } = req.params
  let sql = `select * from blog_articles where category_id = ${id}`
  const data = await getAll(sql)
  res.send({
    code: 0,
    message: '查询成功',
    result: data,
  })
})

// 根据关键字查询该分类下的所有文章
router.post('/search/:id', async (req, res) => {
  const { id } = req.params
  const { keyword } = req.body
  let sql = `select * from blog_articles where category_id = ${id}  and concat(title,author)  like '%${keyword}%'`
  const data = await getAll(sql)
  res.send({
    code: 0,
    message: '查询成功',
    result: data,
  })
})
// 根据关键字查询所有的文章
router.post('/allsearch', async (req, res) => {
  const { keyword } = req.body
  let sql = `select * from blog_articles where concat(title,author) like '%${keyword}%' `
  const data = await getAll(sql)
  res.send({
    code: 0,
    message: '查询成功',
    result: data,
  })
})

module.exports = router
